package com.systop.asset.service.impl;

import com.systop.asset.entity.InventoryCheckDetail;
import com.systop.asset.entity.InventoryRecords;
import com.systop.asset.entity.vo.CheckAssetVo;
import com.systop.asset.entity.vo.CheckMaterialVo;
import com.systop.asset.mapper.InventoryCheckDetailMapper;
import com.systop.asset.mapper.InventoryRecordsMapper;
import com.systop.asset.service.IInventoryCheckDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 盘点单明细Service业务层处理
 * 
 * @author lsp
 * @date 2025-07-07
 */
@Service
public class InventoryCheckDetailServiceImpl implements IInventoryCheckDetailService 
{
    @Autowired
    private InventoryCheckDetailMapper inventoryCheckDetailMapper;
    @Autowired
    private InventoryRecordsMapper inventoryRecordsMapper;



    /**
     * 查询盘点单明细
     * 
     * @param inventoryCheckDetailId 盘点单明细主键
     * @return 盘点单明细
     */
    @Override
    public InventoryCheckDetail selectInventoryCheckDetailByInventoryCheckDetailId(Long inventoryCheckDetailId)
    {
        return inventoryCheckDetailMapper.selectInventoryCheckDetailByInventoryCheckDetailId(inventoryCheckDetailId);
    }

    /**
     * 查询盘点单明细列表
     * 
     * @param inventoryCheckDetail 盘点单明细
     * @return 盘点单明细
     */
    @Override
    public List<InventoryCheckDetail> selectInventoryCheckDetailList(InventoryCheckDetail inventoryCheckDetail)
    {
        return inventoryCheckDetailMapper.selectInventoryCheckDetailList(inventoryCheckDetail);
    }
    @Override
    public List<CheckAssetVo> selectCheckAssetList(InventoryCheckDetail inventoryCheckDetail)
    {
        return inventoryCheckDetailMapper.selectCheckAssetList(inventoryCheckDetail);
    }




    public List<CheckAssetVo> selectCheckAssetBySelf()
    {
        return inventoryCheckDetailMapper.selectCheckAssetBySelf((Long) SecurityContextHolder.getContext().getAuthentication().getCredentials());
    }
    @Override
    public List<CheckMaterialVo> selectCheckMaterial(Long checkId)
    {
        return inventoryCheckDetailMapper.selectCheckMaterial(checkId);
    }

    /**
     * 新增盘点单明细
     * 
     * @param inventoryCheckDetail 盘点单明细
     * @return 结果
     */
    @Override
    public int insertInventoryCheckDetail(InventoryCheckDetail inventoryCheckDetail)
    {
        return inventoryCheckDetailMapper.insertInventoryCheckDetail(inventoryCheckDetail);
    }

    /**
     * 修改盘点单明细
     * 
     * @param inventoryCheckDetail 盘点单明细
     * @return 结果
     */
    @Override
    public int updateInventoryCheckDetail(InventoryCheckDetail inventoryCheckDetail)
    {
        return inventoryCheckDetailMapper.updateInventoryCheckDetail(inventoryCheckDetail);
    }
    @Override
    @Transactional
    public int updateInventoryCheckDetail(InventoryCheckDetail[] inventoryCheckDetails)
    {
        for(InventoryCheckDetail inventoryCheckDetail:inventoryCheckDetails){
            inventoryCheckDetailMapper.updateInventoryCheckDetail(inventoryCheckDetail);
        }
        InventoryRecords inventoryRecords = new InventoryRecords();
        inventoryRecords.setId(inventoryCheckDetails[0].getCheckId());
        inventoryRecords.setIsConfirmed(1);
        return inventoryRecordsMapper.updateInventoryRecords(inventoryRecords);
    }

    /**
     * 批量删除盘点单明细
     * 
     * @param inventoryCheckDetailIds 需要删除的盘点单明细主键
     * @return 结果
     */
    @Override
    public int deleteInventoryCheckDetailByInventoryCheckDetailIds(Long[] inventoryCheckDetailIds)
    {
        return inventoryCheckDetailMapper.deleteInventoryCheckDetailByInventoryCheckDetailIds(inventoryCheckDetailIds);
    }

    /**
     * 删除盘点单明细信息
     * 
     * @param inventoryCheckDetailId 盘点单明细主键
     * @return 结果
     */
    @Override
    public int deleteInventoryCheckDetailByInventoryCheckDetailId(Long inventoryCheckDetailId)
    {
        return inventoryCheckDetailMapper.deleteInventoryCheckDetailByInventoryCheckDetailId(inventoryCheckDetailId);
    }
}
